package flextv.data {

import flash.data.SQLConnection;
import flash.data.SQLStatement;

import flextv.core.logic.Logger;
import flextv.core.vo.Playlist;
import flextv.core.vo.StoredItem;

import flexunit.framework.TestCase;

import mx.collections.ArrayCollection;
import mx.logging.ILogger;

public class PersistenceServiceTest extends TestCase {
	
	private static const LOG: ILogger = Logger.getLog(PersistenceServiceTest);
	
	public function PersistenceServiceTest(methodName: String=null) {
		super(methodName);
		LOG.debug("Creating instance for method ["+methodName+"].");
	}
	
	public var persistenceService: IPersistenceService;
	
	public function testCascadeDelete(): void {
		this.deleteAll((persistenceService as PersistenceServiceImpl).getConnection());
		
		const p1: Playlist = new Playlist("playlist1", new ArrayCollection());
		persistenceService.insertPlaylist(p1);
		
		
		const s1: StoredItem = new StoredItem("yid1", "storeditem1", 0, "file uri");
		persistenceService.insertStoredItem(s1, p1);
		
		const s2: StoredItem = new StoredItem("yid2", "storeditem2", 0, "file uri");
		persistenceService.insertStoredItem(s2, p1);
		
		
		persistenceService.deletePlaylist(p1);
		
		const result: ArrayCollection = persistenceService.fetchPlaylists();
		assertEquals(result.length, 0);
		
	}
	
	private function deleteAll(conn: SQLConnection): void {
		LOG.debug("deleteAll()");
		
		const stmt: SQLStatement = new SQLStatement();
		stmt.sqlConnection = conn;
		stmt.text = "DELETE FROM " + SqlUtil.TBL_PLAYLIST;
		stmt.execute();
		
		stmt.text = "DELETE FROM " + SqlUtil.TBL_STORED_ITEM;
		stmt.execute();
	}
}
}